package com.etrans.web.gps.business.base.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.base.domain.SimCardVO;
import com.etrans.web.gps.business.base.domain.Terminal;
import com.etrans.web.gps.business.base.domain.TerminalKind;


@Service
public class TerminalService extends BaseService{

	public List<Terminal> queryAllTerminals( Map<String, Object> paramMap) {
		List<Terminal> list = selectList("terminalQuery.queryAllTerminalList", paramMap);
		return list;
	}

	public Integer queryTerminalCount( Map<String, Object> paramMap) {
		return (Integer) this.selectOne("terminalQuery.queryTerminalCount", paramMap);
	}

	public Terminal queryTerminalById( Integer terminalId) {
		Terminal terminal = (Terminal) this.selectOne("terminalQuery.queryTerminalById", terminalId);
		return terminal;
	}

	public int queryCountByCOMMNO( HashMap<String, Object> param) {
		return (Integer)this.selectOne("terminalQuery.queryCountByCOMMNO", param);
	}
	
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void saveTerminal( Terminal terminal) {
		SimCardVO simCard = new SimCardVO();
		simCard.setId(terminal.getSimId());
		simCard.setIsInUse(1);
		this.update("updateSimCardStatusSQL", simCard);
		this.insert( "terminalMapper.saveTerminal", terminal);
		
	}

	public void updateTerminal( Terminal terminal) {
		//设置旧的sim卡为未使用
		SimCardVO simCard = new SimCardVO();
		simCard.setId(terminal.getOldSimId());
		simCard.setIsInUse(0);
		this.update("terminalMapper.updateSimCardStatusSQL", simCard);
		//设置新的sim卡为已经使用
		simCard.setId(terminal.getSimId());
		simCard.setIsInUse(1);
		this.update("terminalMapper.updateSimCardStatusSQL", simCard);
		//更新终端信息
		this.update("terminalMapper.updateTerminal", terminal);

	}

	public List<TerminalKind> loadAllTerminalKindTrees() {
		List<TerminalKind> list = selectList("terminalQuery.loadAllTerminalKindTrees",new HashMap());
		return list;
	}

	public void deleteTerminalByID( Integer id) {
		// 更新sim卡状态为未使用
		Terminal terminal = queryTerminalById(id);
		if (terminal.getSimId() != null) {
					SimCardVO simCard = new SimCardVO();
					simCard.setId(terminal.getSimId());
					simCard.setIsInUse(0);
					this.update("updateSimCardStatusSQL", simCard);
		}
		// 删除终端信息
		this.delete("terminalMapper.deleteTerminalByID", id);

	}

	public List<Terminal> queryTerminalExportList( Map<String, Object> paramMap) {
		List<Terminal> list = selectList("terminalQuery.queryTerminalExportList", paramMap);
		return list;
	}

	/**
	 * 修改终端，无sim卡关联信息
	 * @param terminal
	 */
	@Transactional(readOnly = false, rollbackFor = { DataAccessException.class, Exception.class })
	public void updateTerminalInfo( Terminal terminal) {
		this.update("terminalMapper.updateTerminal", terminal);
	}

}
